package tx.bj12320.task;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tx.bj12320.model.SmsInfo;
import tx.bj12320.model.UserInfo;
import tx.bj12320.model.enums.BooleanEnum;
import tx.bj12320.service.SmsInfoManager;
import tx.bj12320.service.UserInfoManager;
import tx.bj12320.util.SmsUtils;
import tx.bj12320.vo.query.UserInfoQuery;

@Service
@Transactional
public class SendSmsTask {
	protected Log log = LogFactory.getLog(getClass());
	@Autowired
	private SmsInfoManager smsInfoManager;
	public void setSmsInfoManager(SmsInfoManager manager) {
		this.smsInfoManager = manager;
	}
	@Autowired
	private UserInfoManager userInfoManager;
	public void setUserInfoManager(UserInfoManager manager) {
		this.userInfoManager = manager;
	}
	public void sendSms(){
		List<SmsInfo> lsis=smsInfoManager.findAllUnSendSms(new UserInfoQuery());
		for (SmsInfo smsInfo : lsis) {
			boolean isSentSuccess=false;
			if(smsInfo.getIsGlobal().equals(BooleanEnum.FALSE.getCode())){//非全局发送
				log.info(smsInfo.getSellPhone()+" - "+smsInfo.getInfoContent());
				isSentSuccess=SmsUtils.sendSms(smsInfo.getSellPhone(), smsInfo.getInfoContent());
			}else if(smsInfo.getIsGlobal().equals(BooleanEnum.TRUE.getCode())){//全局发送
				UserInfoQuery uq=new UserInfoQuery();
				List<UserInfo> luis=userInfoManager.findAllWithSellPhoneIsNotNull(uq);
				for (UserInfo userInfo : luis) {
					log.info("全局短信-"+userInfo.getUserName()+"-"+userInfo.getRealName()+"-"+userInfo.getSellPhone()+"-"+smsInfo.getInfoContent());
					SmsUtils.sendSms(smsInfo.getSellPhone(), smsInfo.getInfoContent());
				}
				isSentSuccess=true;
			}
			if(isSentSuccess){
				smsInfo.setIsSent(1L);
				smsInfoManager.update(smsInfo);
			}
		}
	}
}
